package priv.pic.to.link.main.util;

import com.qiniu.util.Auth;
import com.qiniu.util.StringUtils;
import priv.pic.to.link.main.domain.Config;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * Created on 2018/4/27.
 */
public class PropertiesUtil {

    private static final String PROP_ACCESSKEY = "qiniu.accesskey";
    private static final String PROP_SECRET_KEY = "qiniu.secretkey";
    private static final String PROP_BUCKET = "qiniu.bucket";
    private static final String PROP_IMG_DOMAIN = "qiniu.img.domain";

    private static final String PROP_HOT_KEY_SHUTDOWN = "hot-key.shutdown";
    private static final String PROP_HOT_KEY_UPLOAD = "hot-key.upload";
    private static final String DEFAULT_HOT_KEY_SHUTDOWN = "control+shift+0";
    private static final String DEFAULT_HOT_KEY_UPLOAD = "control+shift+V";

    private static final String EXEC_TYPE = "exec.type";
    private static final String EXE = "exe";

    private static final String DEFAULT_CONFIG_FILE = "D:/tmp/config.properties";
    private static final String EXE_CONFIG_FILE = "config.properties";

    public static Config loadConfig() {
        Config config = new Config();
        //read properties
        try {
            Properties properties = PropertiesUtil.loadProperties();
            String accessKey = properties.getProperty(PROP_ACCESSKEY);
            String secretKey = properties.getProperty(PROP_SECRET_KEY);
            config.bucket = properties.getProperty(PROP_BUCKET);
            config.imgDomain = properties.getProperty(PROP_IMG_DOMAIN);
            config.auth = Auth.create(accessKey, secretKey);

            String shutdownHotKey = properties.getProperty(PROP_HOT_KEY_SHUTDOWN);
            String uploadHotKey = properties.getProperty(PROP_HOT_KEY_UPLOAD);
            config.shutdownHotKey = StringUtils.isNullOrEmpty(shutdownHotKey) ? DEFAULT_HOT_KEY_SHUTDOWN : shutdownHotKey;
            config.uploadHotKey = StringUtils.isNullOrEmpty(uploadHotKey) ? DEFAULT_HOT_KEY_UPLOAD : uploadHotKey;
        } catch (Exception e) {
            e.printStackTrace();
            TipUtil.warn("配置不正确！");
            System.exit(0);
        }
        return config;
    }


    public static Properties loadProperties() {
        FileInputStream fileInputStream = null;
        try {
            String path = DEFAULT_CONFIG_FILE;
            if (EXE.equals(System.getProperties().getProperty(EXEC_TYPE))) {
                path = EXE_CONFIG_FILE;
            }
            File file = new File(path);
            if (!file.exists()) {
                TipUtil.warn(String.format("请在可执行文件所在目录(%s)" +
                        "下添加并填写配置文件：config.properties", file.getParent()));
                System.exit(0);
            }
            Properties properties = new Properties();
            fileInputStream = new FileInputStream(file);
            properties.load(fileInputStream);
            return properties;
        } catch (Exception e) {
            e.printStackTrace();
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e1) {
                }
            }
            TipUtil.warn("配置不正确！");
            System.exit(0);
        }
        return null;
    }
}
